package com.heima.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.crm.common.contents.CommonMessageContent;
import com.heima.crm.common.contents.HoldingConstant;
import com.heima.crm.common.result.PageResult;
import com.heima.crm.common.result.Result;
import com.heima.crm.common.utils.BaseContext;
import com.heima.crm.common.utils.ExcelUtil;
import com.heima.crm.mapper.*;
import com.heima.crm.pojos.dtos.*;
import com.heima.crm.pojos.entity.*;
import com.heima.crm.pojos.vos.ClueFollowVo;
import com.heima.crm.pojos.vos.ImportClueVo;
import com.heima.crm.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CrUserMapper crUserMapper;
    @Autowired
    private TransferMapper transferMapper;


    /**
     * 文件解析
     *
     * @param multipartFile
     * @return
     */
    @Override
    public Result loadFile(MultipartFile multipartFile) {
        ImportClueVo importClueVo = new ImportClueVo();
        try (Workbook workbook = ExcelUtil.getWorkbook(multipartFile)) {

            Sheet sheet = workbook.getSheetAt(0);

            //构建返回参数
            int totalData = 0;
            int sameData = 0;
            int lastRowNum = sheet.getLastRowNum();
            List<Long> ids = new ArrayList<>();


            for (int rowIndex = 1; rowIndex < lastRowNum; rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row != null) {
                    TbClue tbClue = extracted(row);
                    try {
                        CheckData(tbClue);
                    } catch (Exception e) {
                        sameData++;
                        continue;
                    }
                    clueMapper.insert(tbClue);
                    ids.add(tbClue.getId());
                    totalData++;
                }
            }

            importClueVo.setTotalData(totalData);
            importClueVo.setSameData(sameData);
            if (totalData - sameData < 0) {
                importClueVo.setSuccessData(0);
            } else {
                importClueVo.setSuccessData(totalData - sameData);
            }

            importClueVo.setIds(ids);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.success(importClueVo);
    }

    //构建存入数据库的对象
    private TbClue extracted(Row row) {
        Cell phoneCell = row.getCell(0);
        Cell channelCell = row.getCell(1);
        Cell activityCell = row.getCell(2);

//        double numericCellValue = phoneCell.getNumericCellValue();
//        String phone = String.valueOf((long) numericCellValue);
//        log.info("获得的手机号为：{}",phone);

        TbClue tbClue = new TbClue();
        tbClue.setPhone(String.valueOf((long) phoneCell.getNumericCellValue()));
        tbClue.setChannel(String.valueOf((long) channelCell.getNumericCellValue()));
        tbClue.setActivityId(convertCellToLong(activityCell));
        tbClue.setCreateTime(LocalDateTime.now());
        tbClue.setCreateBy(crUserMapper.selectById(BaseContext.getCurrentId()).getUserName());
        tbClue.setStatus(0);
        tbClue.setFalseCount(0);
        tbClue.setUpdateTime(LocalDateTime.now());
        return tbClue;
    }

    //检查数据是否正确，是否是重复数据
    private void CheckData(TbClue tbClue) {
        if (tbClue == null) {
            throw new RuntimeException("数据有误");
        }
        String phone = tbClue.getPhone();
        Integer count = Math.toIntExact(clueMapper.selectCount(new QueryWrapper<TbClue>().eq("phone", phone)));
        if (count > 0) {
            throw new RuntimeException("数据重复");
        }
    }

    public static String convertCellToString(Cell cell) {
        if (cell == null) {
            return null;
        }

        CellType cellType = cell.getCellType();
        String cellValue;

        if (cellType == CellType.STRING) {
            cellValue = cell.getStringCellValue();
        } else if (cellType == CellType.NUMERIC) {
            cellValue = String.valueOf(cell.getNumericCellValue());
        } else if (cellType == CellType.BOOLEAN) {
            cellValue = String.valueOf(cell.getBooleanCellValue());
        } else if (cellType == CellType.FORMULA) {
            cellValue = cell.getCellFormula();
        } else if (cellType == CellType.BLANK) {
            cellValue = "";
        } else {
            cellValue = cell.toString();
        }

        return cellValue;
    }

    public static Long convertCellToLong(Cell cell) {
        Long numericValue = null;

        if (cell != null) {
            CellType cellType = cell.getCellType();

            if (cellType == CellType.NUMERIC) {
                numericValue = (long) cell.getNumericCellValue();
            } else if (cellType == CellType.STRING) {
                try {
                    numericValue = Long.parseLong(cell.getStringCellValue());
                } catch (NumberFormatException e) {
                    // 处理无法解析为长整型的字符串值
                    e.printStackTrace();
                }
            }
        }

        return numericValue;
    }

    /**
     * 删除线索
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteClueByIds(List<Long> ids) {
        if (ids == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }
        clueMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 新增线索
     *
     * @param tbClue
     * @return
     */
    @Override
    public Result newClue(TbClue tbClue) {
        //参数校验
        if (tbClue == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        //判断是否存在该线索
        Integer clueCount = Math.toIntExact(clueMapper.selectCount(new QueryWrapper<TbClue>().eq("phone", tbClue.getPhone())));
        if (clueCount > 0) {
            return Result.error("已存在该线索");
        }

        TbCustomer oldCustomer = tbCustomerMapper.selectOne(new QueryWrapper<TbCustomer>().eq("phone", tbClue.getPhone()));
        if (oldCustomer == null) {
            TbCustomer tbCustomer = new TbCustomer();
            //是新客户
            tbCustomer.setPhone(tbClue.getPhone());
            tbCustomer.setCustomerName(tbClue.getName());
            tbCustomer.setGender(tbClue.getSex().shortValue());
            tbCustomer.setAge(tbClue.getAge());
            tbCustomer.setWechat(tbClue.getWeixin());
            tbCustomer.setQq(tbClue.getQq());
            if ("1".equals(tbClue.getChannel())){
                tbCustomer.setChannel("线上活动");
            }else if ("2".equals(tbClue.getChannel())){
                tbCustomer.setChannel("线上活动");
            }
            tbCustomer.setCreateTime(LocalDateTime.now());
            tbCustomerMapper.insert(tbCustomer);
        }

        //构建存入表的对象，信息补充
        //获取当前登陆人信息
        TbUser tbUser = crUserMapper.selectOne(new QueryWrapper<TbUser>().eq("user_id", BaseContext.getCurrentId()));
        tbClue.setCreateTime(LocalDateTime.now())
                .setFalseCount(0)
                .setUpdateTime(LocalDateTime.now())
                .setTransfer("0")
                .setCreateBy(tbUser.getUserName());


        clueMapper.insert(tbClue);

        TbCustomer customer = tbCustomerMapper.selectOne(new QueryWrapper<TbCustomer>().eq("phone", tbClue.getPhone()));
        customer.setClueId(tbClue.getId());
        tbCustomerMapper.updateById(customer);



        return Result.success();
    }


    /**
     * 线索条件分页查询
     *
     * @param cluePageDto
     * @return
     */
    @Override
    public Result cluePage(CluePageDto cluePageDto) {

        if (cluePageDto == null) {
            throw new RuntimeException("参数有误");
        }

        Page page = new Page(cluePageDto.getPage(), cluePageDto.getSize());
        QueryWrapper<TbClue> queryWrapper = new QueryWrapper<>();

        if (cluePageDto.getId() != null) {
            queryWrapper.eq("id", cluePageDto.getId());
        }
        if (cluePageDto.getPhone() != null) {
            queryWrapper.like("phone", cluePageDto.getPhone());
        }
        if (cluePageDto.getOwner() != null) {
            queryWrapper.like("owner", cluePageDto.getOwner());
        }

        if (cluePageDto.getTime() != null) {
            LocalDateTime startTime = cluePageDto.getTime().atStartOfDay();
            LocalDateTime endTime = cluePageDto.getTime().atTime(LocalTime.MAX);
            queryWrapper.ge("create_time", startTime).le("create_time", endTime);
        }
        if (cluePageDto.getStatus() != null) {
            queryWrapper.eq("status", cluePageDto.getStatus());
        }
        if (cluePageDto.getChannel() != null) {
            queryWrapper.eq("channel", cluePageDto.getChannel());
        }

        page = clueMapper.selectPage(page, queryWrapper);

        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }

    /**
     * 分配线索
     *
     * @param dto
     * @return
     */
    @Override
    public Result distribute(ClueDistributeDto dto) {
        //参数校验
        if (dto == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }
        LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TbTransfer::getUserName, dto.getUserName());
        TbTransfer tbTransfer = transferMapper.selectOne(lqw);

        if (tbTransfer.getClueNum() + 1 > 100) {
            return Result.error("保有量已达上线,对多可分配" + (HoldingConstant.HOLDING_COUNT - tbTransfer.getBusyNum()) + "条");


        }
        //构建参数
        TbClue selectById = clueMapper.selectById(dto.getId());
        selectById.setUpdateTime(LocalDateTime.now()).setOwner(dto.getUserName()).setStatus(1);

        //更新  状态和拥有者（owner）
        clueMapper.updateById(selectById);
        //更新转派表
        tbTransfer.setClueNum(tbTransfer.getClueNum() + 1);
        tbTransfer.setUpdateTime(LocalDateTime.now());
        tbTransfer.setUpdateId(BaseContext.getCurrentId());
        transferMapper.updateById(tbTransfer);
        return Result.success();
    }

    /**
     * 跟进 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public Result followClue(Long id) {
        //参数校验
        if (id == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        TbClue tbClue = clueMapper.selectOne(new QueryWrapper<TbClue>().eq("id", id));
        if (tbClue == null){
            return Result.error(CommonMessageContent.INVALID_PARAMETER);
        }
        List<TbClueFollow> clueFollowList = clueFollowMapper.selectList(new QueryWrapper<TbClueFollow>().eq("clue_id", id));
        ClueFollowVo clueFollowVo = new ClueFollowVo();
        clueFollowVo.setTbClue(tbClue);
        clueFollowVo.setTbClueFollowList(clueFollowList);

        return Result.success(clueFollowVo);
    }


    @Autowired
    private ClueFollowMapper clueFollowMapper;

    /**
     * 添加跟进
     *
     * @param dto
     * @return
     */
    @Override
    public Result saveFollowClue(ClueFollowDto dto) {
        //验证参数
        if (dto == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }
        TbClue oldClue = clueMapper.selectById(dto.getClueId());
        TbCustomer oldCustomer = tbCustomerMapper.selectOne(new QueryWrapper<TbCustomer>().eq("phone", oldClue.getPhone()));
        if (oldCustomer== null){
            TbCustomer tbCustomer = new TbCustomer();
            tbCustomer.setClueId(dto.getClueId());
            tbCustomer.setCustomerName(dto.getName());
            if ("1".equals(oldClue.getChannel())){
                tbCustomer.setChannel("线上活动");
            }else if ("2".equals(oldClue.getChannel())){
                tbCustomer.setChannel("线上活动");
            }
            tbCustomer.setGender(dto.getSex().shortValue());
            tbCustomer.setAge(dto.getAge());
            tbCustomer.setWechat(dto.getWeixin());
            tbCustomer.setQq(dto.getQq());
            tbCustomer.setPhone(oldClue.getPhone());
            tbCustomer.setCreateTime(LocalDateTime.now());
            tbCustomerMapper.insert(tbCustomer);
        }


        Long id = BaseContext.getCurrentId();
        TbUser tbUser = crUserMapper.selectById(id);

        //构建tbclue对象
        TbClue tbClue = new TbClue()
                .setId(dto.getClueId())
                .setName(dto.getName())
                .setQq(dto.getQq())
                .setWeixin(dto.getWeixin())
                .setSex(dto.getSex())
                .setAge(dto.getAge())
                .setStatus(2);
        //构建跟进表对象
        TbClueFollow tbClueFollow = new TbClueFollow()
                .setClueId(dto.getClueId())
                .setLevel(dto.getLevel())
                .setSubjectId(dto.getSubjectId())
                .setFollowRecord(dto.getFollowRecord())
                .setNextTime(dto.getNextTime())
                .setOperator(tbUser.getUserName())
                .setOperateTime(LocalDateTime.now());

        //更新clue表中的数据，状态更改为 2 进行中
        clueMapper.updateById(tbClue);
        //在跟进表中
        clueFollowMapper.insert(tbClueFollow);

        return Result.success();
    }


    @Autowired
    private CluePoolMapper cluePoolMapper;

    /**
     * 伪线索上报
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result fakeClue(ClueFakeDto dto) {
        //参数校验
        if (dto == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        TbClue tbClue = clueMapper.selectById(dto.getId());
        //校验伪线索次数
        if (tbClue.getFalseCount() == 2) {
            //本来已经是两次，此次为第三次，在线索表及线索跟进表中删除，存入线索池中

            //删除线索表
            clueMapper.deleteById(dto.getId());
            //删除跟进表
            clueFollowMapper.delete(new QueryWrapper<TbClueFollow>().eq("clue_id", dto.getId()));
            //删除线索池
            cluePoolMapper.delete(new QueryWrapper<TbCluePool>().eq("clue_id", dto.getId()));

            //更新转派表
            LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
            lqw.eq(TbTransfer::getUserName, tbClue.getOwner());
            TbTransfer tbTransfer = transferMapper.selectOne(lqw);
            tbTransfer.setClueNum(tbTransfer.getClueNum() - 1);
            tbTransfer.setUpdateTime(LocalDateTime.now());
            tbTransfer.setUpdateId(BaseContext.getCurrentId());
            transferMapper.updateById(tbTransfer);
            return Result.error("伪线索上报达3此，已删除线索");
        }

        //第一次或第二次提交伪线索，将线索存入线索池
        if (tbClue.getFalseCount() == 0) {
            TbCluePool tbCluePool = new TbCluePool()
                    .setClueId(tbClue.getId())
                    .setActivityId(tbClue.getActivityId())
                    .setChannel(tbClue.getChannel())
                    .setPhone(tbClue.getPhone())
                    .setCreateTime(LocalDateTime.now());

            cluePoolMapper.insert(tbCluePool);

        }

        Long userId = BaseContext.getCurrentId();
        TbUser tbUser = crUserMapper.selectById(userId);
        //在跟进表中添加伪线索信息
        TbClueFollow tbClueFollow = new TbClueFollow()
                .setClueId(tbClue.getId())
                .setOperator(tbUser.getUserName())
                .setOperateTime(LocalDateTime.now())
                .setFakeReason(dto.getFakeReason())
                .setFakeRemark(dto.getFakeRemark());
        clueFollowMapper.insert(tbClueFollow);

        //修改线索表中的伪线索提交次数
        tbClue.setFalseCount(tbClue.getFalseCount() + 1);
        //修改线索表中线索的状态
        //tbClue.setStatus(4);
        clueMapper.updateById(tbClue);

        return Result.success();
    }

    @Autowired
    private TbCustomerMapper tbCustomerMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;


    /**
     * 线索转商机
     *
     * @param id
     * @return
     */
    @Override
    public Result clueToBusiness(Long id) {

        if (id == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        //根据id查出线索详情
        TbClue tbClue = clueMapper.selectById(id);
        //封装商机对象  其中需要补充的数据： 客户id、归属人id  需要设置的数据：status、createTime
        TbUser tbUser = crUserMapper.selectOne(new QueryWrapper<TbUser>().eq("user_name", tbClue.getOwner()));

        TbCustomer tbCustomer = tbCustomerMapper.selectOne(new QueryWrapper<TbCustomer>().eq("phone", tbClue.getPhone()));
        //TbClueFollow tbClueFollow = clueFollowMapper.selectOne(new QueryWrapper<TbClueFollow>().eq("clue_id", id));
        Integer subjectId = null;
        List<TbClueFollow> tbClueFollows = clueFollowMapper.selectList(new QueryWrapper<TbClueFollow>().eq("clue_id", id));
        for (TbClueFollow tbClueFollow : tbClueFollows) {
            if (tbClueFollow.getSubjectId() != null) {
                subjectId = tbClueFollow.getSubjectId();
                break;
            }
        }
        TbBusiness tbBusiness = new TbBusiness()
                .setCustomerId(tbCustomer.getId())
                .setCustomerName(tbCustomer.getCustomerName())
                .setPhone(tbClue.getPhone())
                .setAscriptionId(tbUser.getUserId())
                .setAscriptionName(tbUser.getUserName())
                .setDisciplineId(subjectId)
                .setStatus((short) 0)
                .setCreateTime(LocalDateTime.now());

        tbBusinessMapper.insert(tbBusiness);

        return Result.success();
    }


    /**
     * 线索池条件分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result cluePoolList(CluePoolPageDto dto) {

        if (dto == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }
        Page page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<TbCluePool> queryWrapper = new QueryWrapper<>();
        if (dto.getId() != null) {
            queryWrapper.eq("clue_id", dto.getId());
        }
        if (dto.getPhone() != null) {
            queryWrapper.like("phone", dto.getPhone());
        }
        if (dto.getChannel() != null) {
            queryWrapper.eq("channel", dto.getChannel());
        }
        if (dto.getTime() != null) {
            LocalDateTime startTime = dto.getTime().atStartOfDay();
            LocalDateTime endTime = dto.getTime().atTime(LocalTime.MAX);
            queryWrapper.ge("create_time", startTime).le("create_time", endTime);
        }
        page = cluePoolMapper.selectPage(page, queryWrapper);

        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }

    /**
     * 捞取线索
     *
     * @param id
     * @return
     */
    @Override
    public Result clueFish(Long id) {

        if (id == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        //删除线索池
        cluePoolMapper.deleteById(id);
        //根据线索id查询线索
        TbClue tbClue = clueMapper.selectById(id);

        //取当前捞取者的id  再去到其详细信息
        Long userId = BaseContext.getCurrentId();
        TbUser tbUser = crUserMapper.selectById(userId);
        //修改线索的拥有者和状态
        tbClue.setOwner(tbUser.getUserName()).setStatus(1).setFalseCount(0);
        //修改表
        clueMapper.updateById(tbClue);
        //更新转派表
        //看看捞取的是不是本人的伪线索,因为伪线索也被人拥有,但是在线索池中
        //
        if (tbClue.getOwner() == null) {
            LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
            lqw.eq(TbTransfer::getUserName, tbUser.getUserName());
            TbTransfer tbTransfer = transferMapper.selectOne(lqw);
            if (tbTransfer.getClueNum() + 1 > 100) {
                return Result.error("保有量已达上线,对多可分配" + (HoldingConstant.HOLDING_COUNT - tbTransfer.getBusyNum()) + "条");


            }
            tbTransfer.setClueNum(tbTransfer.getClueNum() + 1);
            tbTransfer.setUpdateTime(LocalDateTime.now());
            tbTransfer.setUpdateId(BaseContext.getCurrentId());
            transferMapper.updateById(tbTransfer);
        } else {
            //是伪线索,原拥有人线索-1,
            LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
            lqw.eq(TbTransfer::getUserName, tbClue.getOwner());
            TbTransfer tbTransfer = transferMapper.selectOne(lqw);
            tbTransfer.setClueNum(tbTransfer.getClueNum() - 1);
            tbTransfer.setUpdateTime(LocalDateTime.now());
            tbTransfer.setUpdateId(BaseContext.getCurrentId());
            transferMapper.updateById(tbTransfer);
            //现拥有人+1
            LambdaQueryWrapper<TbTransfer> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(TbTransfer::getUserName, tbUser.getUserName());
            TbTransfer tbTransfer1 = transferMapper.selectOne(lqw1);
            if (tbTransfer1.getClueNum() + 1 > 100) {
                return Result.error("保有量已达上线,对多可分配" + (HoldingConstant.HOLDING_COUNT - tbTransfer.getBusyNum()) + "条");
            }
            tbTransfer1.setClueNum(tbTransfer1.getClueNum() + 1);
            tbTransfer1.setUpdateTime(LocalDateTime.now());
            tbTransfer1.setUpdateId(BaseContext.getCurrentId());
            transferMapper.updateById(tbTransfer1);
        }
        return Result.success();
    }

    /**
     * 批量捞取线索
     *
     * @param ids
     * @return
     */
    @Override
    public Result clueFishByIds(List<Long> ids) {

        if (ids == null) {
            return Result.error(CommonMessageContent.PARAMETER_EXCEPTION);
        }

        for (Long id : ids) {
            //删除线索池
            cluePoolMapper.deleteById(id);
            //根据线索id查询线索
            TbClue tbClue = clueMapper.selectById(id);
            //取当前捞取者的id  再去到其详细信息
            Long userId = BaseContext.getCurrentId();
            TbUser tbUser = crUserMapper.selectById(userId);
            //修改线索的拥有者和状态
            tbClue.setOwner(tbUser.getUserName()).setStatus(1).setFalseCount(0);
            //修改表
            clueMapper.updateById(tbClue);
            //更新转派表
            //看看捞取的是不是本人的伪线索,因为伪线索也被人拥有,但是在线索池中
            //
            if (tbClue.getOwner() == null) {
                LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
                lqw.eq(TbTransfer::getUserName, tbUser.getUserName());
                TbTransfer tbTransfer = transferMapper.selectOne(lqw);
                if (tbTransfer.getClueNum() + 1 > 100) {
                    return Result.error("保有量已达上线,对多可分配" + (HoldingConstant.HOLDING_COUNT - tbTransfer.getBusyNum()) + "条");


                }
                tbTransfer.setClueNum(tbTransfer.getClueNum() + 1);
                tbTransfer.setUpdateTime(LocalDateTime.now());
                tbTransfer.setUpdateId(BaseContext.getCurrentId());
                transferMapper.updateById(tbTransfer);
            } else {
                //是伪线索,原拥有人线索-1,
                LambdaQueryWrapper<TbTransfer> lqw = new LambdaQueryWrapper<>();
                lqw.eq(TbTransfer::getUserName, tbClue.getOwner());
                TbTransfer tbTransfer = transferMapper.selectOne(lqw);
                tbTransfer.setClueNum(tbTransfer.getClueNum() - 1);
                tbTransfer.setUpdateTime(LocalDateTime.now());
                tbTransfer.setUpdateId(BaseContext.getCurrentId());
                transferMapper.updateById(tbTransfer);
                //现拥有人+1
                LambdaQueryWrapper<TbTransfer> lqw1 = new LambdaQueryWrapper<>();
                lqw1.eq(TbTransfer::getUserName, tbUser.getUserName());
                TbTransfer tbTransfer1 = transferMapper.selectOne(lqw1);
                if (tbTransfer.getClueNum() + 1 > 100) {
                    return Result.error("保有量已达上线,对多可分配" + (HoldingConstant.HOLDING_COUNT - tbTransfer.getBusyNum()) + "条");


                }
                tbTransfer1.setClueNum(tbTransfer1.getClueNum() + 1);
                tbTransfer1.setUpdateTime(LocalDateTime.now());
                tbTransfer1.setUpdateId(BaseContext.getCurrentId());
                transferMapper.updateById(tbTransfer1);
            }

        }

        return Result.success();
    }
}
